package com.dc.controller;

import com.dc.annotation.Log;
import com.dc.annotation.UnCheckToken;
import com.dc.common.ResponseResultVO;
import com.dc.entity.ro.DictionaryRO;
import com.dc.service.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/diction")
@Api(tags = "数据字典相关接口")
public class DictionaryController {

    @Resource
    DictionaryService dictionaryService;

    @ApiOperation(value = "查看数据列表", notes = "查看列表")
    @RequestMapping(value = "/selectDictionaryList", method = RequestMethod.POST)
    public ResponseResultVO selectDictionaryList(@RequestBody(required = false) DictionaryRO dictionaryRO) {
        return dictionaryService.selectDictionaryList(dictionaryRO);
    }

    @UnCheckToken(name = "/dc-control-system", url = "/diction/selectDictionaryByType")
    @ApiOperation(value = "查看单个数据类型", notes = "查看单个数据类型")
    @RequestMapping(value = "/selectDictionaryByType", method = RequestMethod.GET)
    @Cacheable(value = "selectDictionaryByType", unless = "#result.state != 200")
    public ResponseResultVO selectDictionaryByType(@RequestParam String type) {
        return dictionaryService.selectDictionaryByType(type);
    }

    @ApiOperation(value = "查看所有数据字典数据列表", notes = "查看所有数据字典数据列表")
    @RequestMapping(value = "/selectAllDictionaryList", method = RequestMethod.GET)
    @Cacheable(value = "selectAllDictionaryList", unless = "#result.state != 200")
    public ResponseResultVO selectAllDictionaryList() {
        return dictionaryService.selectAllDictionaryList();
    }

    @Log(title = "数据字典", businessDesc = "新增数据字典")
    @ApiOperation(value = "新增数据", notes = "新增数据")
    @RequestMapping(value = "/insertDictionary", method = RequestMethod.POST)
    @Caching(evict = {
            @CacheEvict(value = "selectDictionaryByType", allEntries = true),
            @CacheEvict(value = "selectAllDictionaryList", allEntries = true)
    })
    public ResponseResultVO insertDictionary(@RequestBody DictionaryRO dictionaryRO) {
        return dictionaryService.insertDictionary(dictionaryRO);
    }

    @Log(title = "数据字典", businessDesc = "修改数据")
    @ApiOperation(value = "修改数据", notes = "修改数据")
    @RequestMapping(value = "/updateDictionary", method = RequestMethod.POST)
    @Caching(evict = {
            @CacheEvict(value = "selectDictionaryByType", allEntries = true),
            @CacheEvict(value = "selectAllDictionaryList", allEntries = true)
    })
    public ResponseResultVO updateDictionary(@RequestBody DictionaryRO dictionaryRO) {
        return dictionaryService.updateDictionary(dictionaryRO);
    }

    @Log(title = "数据字典", businessDesc = "删除数据")
    @ApiOperation(value = "删除数据", notes = "删除数据")
    @RequestMapping(value = "/deleteDictionaryById", method = RequestMethod.GET)
    @CacheEvict(value = {"selectDictionaryByType"})
    @Caching(evict = {
            @CacheEvict(value = "selectDictionaryByType", allEntries = true),
            @CacheEvict(value = "selectAllDictionaryList", allEntries = true)
    })
    public ResponseResultVO deleteDictionaryById(@RequestParam String id) {
        return dictionaryService.deleteDictionaryById(id);
    }
}
